package com.apes.scm.warning;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.service.PiManager;
import com.apes.scm.contract.repository.ContractRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("contractWarningService")
public class ContractWarningService extends DomainService {

    @Autowired
    private ContractRepository contractRepository;
    /**
     * 查询合同预警汇总
     * @return
     */
    public JSONArray qry30EffectiveContractSum(SimpleRequest request) {
        JSONArray array = new JSONArray();
        JSONObject singleItem = new JSONObject();
        singleItem.put("LEFTTEXT", "合同到期预警");
        List<Map> get30EffectiveContract = contractRepository.query30EffectiveContract();
        if (get30EffectiveContract.size() != 0) {
            singleItem.put("RIGHTTEXT", Integer.valueOf(contractRepository.query30EffectiveContract().get(0).get("TOTAL").toString()));
        } else {
            singleItem.put("RIGHTTEXT", 0);
        }
        array.add(singleItem);
        return array;
    }

    /**
     * 查询 30 天快要过期合同
     * @param request
     * @return
     */
    public Page<Object> query30EffectiveContract(SimpleRequest request) {
        List<Map> qry30Effective = contractRepository.query30EffectiveContract();
        Pageable pageable = PageRequest.of(((Integer) request.get("page")) - 1, request.get("size"));
        if (qry30Effective.size() != 0) {
            int total = Integer.valueOf(qry30Effective.get(0).get("TOTAL").toString());
            PageImpl<Object> pPage = new PageImpl(qry30Effective, pageable, qry30Effective.size() == 0 ? 0 : total);
            return pPage;
        }
        return new PageImpl(qry30Effective, pageable, qry30Effective.size() == 0 ? 0 : 0);
    }
}
